package com.zb.frame.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zb.frame.base.model.MPPage;
import com.zb.frame.modules.sys.entity.ParamType;
import com.zb.frame.modules.sys.entity.Role;
import com.zb.frame.modules.sys.mapper.ParamTypeMapper;
import com.zb.frame.modules.sys.service.ParamTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bangZZ
 * @since 2020-09-07
 */
@Service
public class ParamTypeServiceImpl extends ServiceImpl<ParamTypeMapper, ParamType> implements ParamTypeService {

    @Override
    public MPPage getParamTypeList(Page<ParamType> pageInfo, Map<String, Object> params) {
        QueryWrapper<ParamType> wrapper = new QueryWrapper();
        if (StringUtils.isNotBlank((String)params.get("name"))) {
            wrapper.and(qw -> qw.like("pa_name", params.get("name")).or().like("pa_alias", params.get("name")));
        }
        wrapper.orderByDesc("create_time");
        IPage<ParamType> page = this.page(pageInfo, wrapper);
        return new MPPage(page);
    }

    @Override
    public ParamType addParamType(ParamType data) {
        ParamType paramType = new ParamType();
        paramType.setPaName(data.getPaName());
        paramType.setPaAlias(data.getPaAlias());
        paramType.setIsSys(0);
        return this.save(paramType) ? paramType : null;
    }

    @Override
    public ParamType updateParamType(ParamType data) {
        ParamType paramType = new ParamType();
        paramType.setPaId(data.getPaId());
        paramType.setPaAlias(data.getPaAlias());
        boolean f = this.updateById(paramType);
        return f ? paramType : null;
    }

    @Override
    public boolean isExist(String name) {
        QueryWrapper<ParamType> wrapper = new QueryWrapper<ParamType>();
        wrapper.eq("pa_name", name);
        return this.count(wrapper) > 0;
    }


}
